package cn.xuewenbao.eduorder.controller;


import cn.xuewenbao.eduorder.entity.Eduorder;
import cn.xuewenbao.eduorder.service.EduorderService;
import cn.xuewenbao.eduorder.service.PayLogService;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/eduorder/paylog")
@Api(tags = "支付日志表")
@Slf4j
public class PayLogController {

    @Autowired
    private PayLogService payLogService;
    @Autowired
    private EduorderService orderService;

    //生成微信支付二维码接口
    //参数是订单号
    @ApiOperation(value = "生成支付二维码接口,参数是支付平台和订单号")
    @GetMapping("createNative/{provider}/{orderNo}")
    public Result<Map> createNative(@PathVariable String provider, @PathVariable String orderNo) {
        //返回信息，包含二维码地址，还有其他需要的信息
        Result<Map> result = payLogService.createNatvie(orderNo);
        return result;
    }

    //返回微信APP支付所需的信息，统一下单接口
    @ApiOperation(value = "返回微信APP支付所需的信息，统一下单接口")
    @GetMapping("createAppPay/{orderNo}")
    public Result<Map> createAppPay(@PathVariable String orderNo, HttpServletRequest request) {
        Result<Map> result = payLogService.createAppPay(orderNo, request);
        return result;
    }

    //返回支付宝APP支付所需的信息
    @ApiOperation(value = "返回支付宝APP支付所需的信息")
    @GetMapping("createALiPay/{orderNo}")
    public Result<Map> createALiPay(@PathVariable String orderNo) {
        Result<Map> result = null;
        try {
            result = payLogService.createALiPay(orderNo);
        } catch (AlipayApiException e) {
            log.error(e.toString());
        }
        return result;
    }


    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @ApiOperation(value = "查询订单支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public Result<?> queryPayStatus(@PathVariable String orderNo) {
        Result<Eduorder> result = new Result<>();

        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        if (map == null) {
            result.setSuccess(false);
            result.setMessage("支付出错了");
            return result;
        }
        //如果返回map里面不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) {//支付成功
//            添加记录到支付表，更新订单表订单状态
            payLogService.updateOrdersStatus(map);
//            获取支付后的订单信息
            QueryWrapper<Eduorder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_no", orderNo);
            Eduorder order = orderService.getOne(wrapper);
            result.setSuccess(true);
            result.setMessage("支付成功");
            result.setResult(order);
            return result;
        }
        result.setSuccess(false);
        result.setMessage("支付中，请稍后再试");
        return result;
    }

    //查询支付宝订单状态
    @ApiOperation(value = "查询支付宝订单状态")
    @GetMapping("queryAliPayStatus/{orderNo}")
    public Result<Eduorder> queryAliPayStatus(@PathVariable String orderNo) {
        return payLogService.queryAliPayStatus(orderNo);
    }

    //支付宝回调
    @ApiOperation(value = "支付宝回调")
    @PostMapping("notifyAli")
    public String notifyAli(HttpServletRequest request, HttpServletResponse response){
        return payLogService.notifyAli(request,response);
    }

}

